from selenium import webdriver as wd
from time import sleep
from selenium.webdriver.support.select import Select

driver = wd.Chrome()
# set windown max
# driver.maximize_window()
# sleep('3')
driver.get("http://localhost:81/ranzhi/www/sys/user-login.html")
'''
在这输入要访问的网址
'''

# #set windown max
# driver.maximize_window()
# sleep('5')

font = driver.find_element_by_xpath('//*[@id="langs"]/button')
font.click()
sleep(3)
# choose=driver.find_element_by_xpath('//*[@id="langs"]/ul/li[2]/a')
# choose.click()
choose = driver.find_element_by_css_selector('#langs > ul > li:nth-child(3) > a')
choose.click()
sleep(2)
account = driver.find_element_by_xpath('//*[@id="account"]')
account.send_keys('11615169')
sleep(2)
password = driver.find_element_by_xpath('//*[@id="password"]')
password.send_keys('302916')
driver.find_element_by_xpath('//*[@id="keepLoginon"]').click()
sleep(2)
submit = driver.find_element_by_xpath('//*[@id="submit"]')
submit.click()
sleep(3)

username = driver.find_element_by_xpath('//*[@id="mainNavbar"]/div/ul[1]/li/a')
print('current user is:' + username.text)
print("the current browser URL is :" + driver.current_url)
print("the current browser TITLE is:" + driver.title)

# go into Admin setting
admin = driver.find_element_by_xpath('//*[@id="s-menu-superadmin"]/button/i')
admin.click()
print("current URL is :" + driver.current_url)
print("brower TITLE is :" + driver.title)
sleep(3)
ad_iframe = driver.find_element_by_xpath('//*[@id="iframe-superadmin"]')
driver.switch_to.frame(ad_iframe)
ad_user = driver.find_element_by_xpath('//*[@id="mainNavbar"]/ul/li[2]/a')
ad_user.click()
# go to add user
ad_adduse = driver.find_element_by_xpath('/html/body/div/div/div/div[1]/div/div[2]/a[1]')
ad_adduse.click()

ad_account = driver.find_element_by_xpath('//*[@id="account"]')
ad_account.send_keys('jiabaoyu4')
ad_realname = driver.find_element_by_xpath('//*[@id="realname"]')
ad_realname.send_keys('贾宝玉4')

ad_role = driver.find_element_by_xpath('//*[@id="role"]')
choose_role = Select(ad_role)
choose_role.select_by_value('sale')
ad_gender = driver.find_element_by_xpath('//*[@id="genderm"]')
ad_gender.click()

ad_dept = driver.find_element_by_xpath('//*[@id="dept"]')
choose_dept = Select(ad_dept)
choose_dept.select_by_value("10")

ad_pw = driver.find_element_by_css_selector('#password1')
ad_pw.send_keys('123456')
ad_rpw = driver.find_element_by_css_selector('#password2')
ad_rpw.send_keys('123456')
ad_email = driver.find_element_by_xpath('//*[@id="email"]')
ad_email.send_keys('sldksanmjlks4@qq.com')

sleep(3)
ad_submit = driver.find_element_by_xpath('//*[@id="submit"]')
ad_submit.click()
sleep(3)

# exit Admin
driver.switch_to.default_content()

# signout
signout = driver.find_element_by_xpath('//*[@id="bottomRightBar"]/ul/li[1]/a')
signout.click()
sleep(3)

# set windown max
# driver.maximize_window()
# sleep('5')

# login agin with new admin_user
font = driver.find_element_by_xpath('//*[@id="langs"]/button')
font.click()
sleep(3)
choose = driver.find_element_by_xpath('//*[@id="langs"]/ul/li[2]/a')
choose.click()

sleep(2)
account = driver.find_element_by_xpath('//*[@id="account"]')
account.send_keys('jiabaoyu3')
sleep(2)
password = driver.find_element_by_xpath('//*[@id="password"]')
password.send_keys('123456')
driver.find_element_by_xpath('//*[@id="keepLoginon"]').click()
sleep(2)
submit = driver.find_element_by_xpath('//*[@id="submit"]')
submit.click()
sleep(3)
username = driver.find_element_by_xpath('//*[@id="mainNavbar"]/div/ul[1]/li/a')
print('current user is:' + username.text)
print('new Admin_user is useful')
sleep(3)
driver.quit()
